//
//  HomeService.swift
//  UAKA
//
//  Created by 王剑鹏 on 2020/4/26.
//  Copyright © 2020 waing. All rights reserved.
//

import UIKit
import Moya
import ObjectMapper


// MARK: Service
enum HomeService  {
    case demo1
    case demo22(name: String, age: Int)
}

extension HomeService: TargetType {
    // 请求服务器的根路径
    var baseURL: URL { return baseUrl }

    // 每个API对应的具体路径
    var path: String {
        switch self {
        case .demo1:
            return ""
        case .demo22:
            return ""
        }
        
    }
    
    // 各个接口的请求方式，get或post
    var method: Moya.Method {
        switch self {
        case .demo1,.demo22:
            return .get
        }
    }

    // 请求是否携带参数，如果需要参数，就做如demo2和demo3的设置
    var task: Task {
        switch self {
        case .demo1:
            return .requestParameters(parameters: BaseParameters.setRoute("app/getData"),
                                      encoding: URLEncoding.default)
        case .demo22(let name, let age):
            let param = BaseParameters.setRoute("app/getData").addParams(key: "name", value: name).addParams(key: "age", value: age)
            debugPrint("参数===>>>> \(param)")
            return .requestParameters(parameters: param,
                                                 encoding: URLEncoding.default)
        }
    }
    
    // 单元测试使用
    var sampleData: Data {
        switch self {
        case .demo1:
            return Data()
        case .demo22:
            return Data()
        }
    }

    // 请求头
    var headers: [String : String]? {
        return nil
    }
}


// MARK: Model
class HomeData: Mappable {
    required init?(map: Map) {
        
    }
    func mapping(map: Map) {
        sidebar     <- map["sidebar"]
    }
    var sidebar : [SidebarItem] = [SidebarItem]()
}

class SidebarItem: Mappable {
    required init?(map: Map) {
        
    }
    func mapping(map: Map) {
        name      <- map["name"]
        type        <- map["type"]
        flag        <- map["flag"]
    }
    var name: String = ""
    var type: String = ""
    var flag: Bool = false
    var isList: Bool = false
    var catId: Int = 1
}
